package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.ProjectPlansDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.ProjectPlans;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingLevelVo;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.ProjectPlansVo;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    NursingPlanMapper nursingPlanMapper;


    @Override
    public PageResponse<NursingPlanVo> pageFind(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> pages = nursingPlanMapper.pageFind(name, status);
        PageResponse<NursingPlanVo> bean = PageResponse.of(pages, NursingPlanVo.class);
        return bean;
    }

    @Override
    public NursingPlanVo ById(Integer id) {
        NursingPlan nursingPlan = nursingPlanMapper.ById(id);
            NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);


            List<ProjectPlans> projectPlans = nursingPlanMapper.findProjectPlans(id);
            List<ProjectPlansVo> ppd=projectPlans.stream().map(projectPlans1 -> {
                ProjectPlansVo projectPlansVo = BeanUtil.toBean(projectPlans1, ProjectPlansVo.class);
                return projectPlansVo;
            }).collect(Collectors.toList());

            nursingPlanVo.setProjectPlans(ppd);


            return nursingPlanVo;


    }

    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.update(nursingPlan);

        nursingPlanMapper.deletePlans(nursingPlan.getId());

        List<ProjectPlans> planProjects = nursingPlanDto.getProjectPlans().stream().map(nursingPlanProjectDto -> {
            ProjectPlans nursingPlanProject = BeanUtil.toBean(nursingPlanProjectDto, ProjectPlans.class);
            nursingPlanProject.setPlanId(nursingPlan.getId());
            return nursingPlanProject;
        }).collect(Collectors.toList());

        nursingPlanMapper.addPlans(planProjects);
    }

    @Override
//    @Transactional(rollbackFor = Exception.class)
    public void add(NursingPlanDto nursingPlanDto) {

        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlanMapper.add(nursingPlan);

        List<ProjectPlans> planProjects = nursingPlanDto.getProjectPlans().stream()
                .map(nursingPlanProjectDto -> {
                    ProjectPlans nursingPlanProject = new ProjectPlans();
                    BeanUtils.copyProperties(nursingPlanProjectDto, nursingPlanProject);
                    nursingPlanProject.setPlanId(nursingPlan.getId());
                    return nursingPlanProject;
                }).collect(Collectors.toList());
        nursingPlanMapper.addPlans(planProjects);  
        }

    @Override
    public void delete(Long id) {
        nursingPlanMapper.delete(id);

        nursingPlanMapper.deletePlans(id);
    }



    @Override
    public void updateNursingPlanStatus(Integer id, Integer status) {
        nursingPlanMapper.updateStatus(id, status);
    }

    @Override
    public List<NursingPlanVo> getNursingPlanList() {
        List<NursingPlan> nursingLevel = nursingPlanMapper.selectAll();
        List<NursingPlanVo> ts = BeanUtil.copyToList(nursingLevel, NursingPlanVo.class);
        return ts;
    }


}

